home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_300 / 352_01 / strppins.cpp < prev    next >
C/C++ Source or Header  |  1991-04-28  |  806b  |  32 lines

  1. // STRPPINS.CPP  - contains String::insert()
  2. //        routine to insert one string into another.
  3. //
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <alloc.h>
  7. #include <iostream.h>
  8. #include <ctype.h>
  9.  
  10. #include "dblib.h"
  11.  
  12. String&  String::insert ( char *ins, int pos )
  13.     // insert string ins into current string at position pos.
  14.     // alters the existing string.
  15.     {
  16.     char *old_s;
  17.     int old_n;
  18.     old_n =n;
  19.     old_s = s;        // hold onto original string contents.
  20.     int ins_n = strlen(ins);
  21.     if (!( ins_n==0 || ins== NULL || pos > old_n ))
  22.         {    
  23.         n = old_n + ins_n;    // new size.
  24.         construct ( old_s );    // larger buffer with old string in it.
  25.         memcpy ( s+pos, ins, ins_n );
  26.         memcpy ( s+pos+ ins_n, old_s +pos, old_n -pos );
  27.         free ( old_s );
  28.         }
  29.     return *this;
  30.     }        // end of String::insert()
  31.  
  32.